package com.naiterui.ehp.bs.cms.modules.base.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.base.service.IAgreementService;
import com.naiterui.ehp.bs.cms.modules.base.vo.AgreementPageVO;
import com.naiterui.ehp.bs.cms.modules.base.vo.AgreementVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 协议管理
 * 
 * @author yangfuya
 * @since 2021/10/12
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGREEMENT})
@AllArgsConstructor
@RestController
@RequestMapping("/base/agreement")
@PreAuthorize("@p.check('base:agreement')")
public class AgreementController {
    private final IAgreementService iAgreementService;

    @SysLog("协议列表")
    @ApiOperation("协议列表")
    @ApiImplicitParam(name = "title", value = "协议标题", dataType = "string", paramType = "query")
    @GetMapping("/list")
    // @PreAuthorize("@p.check('base:agreement:list')")
    public PageVO<AgreementPageVO> list(String title, PageParamsVO pageParamsVO) {
        return iAgreementService.queryPage(title, pageParamsVO);
    }

    @SysLog("协议详情")
    @GetMapping("/detail/{agreementId}")
    @ApiOperation("协议详情")
    @ApiImplicitParam(name = "agreementId", value = "协议id", dataType = "long", paramType = "path", required = true)
    // @PreAuthorize("@p.check('base:agreement:detail')")
    public AgreementVO details(@PathVariable("agreementId") Long agreementId) throws BusinessException {
        return iAgreementService.getById(agreementId);
    }

    @SysLog("新增协议")
    @ApiOperation("新增协议")
    @PostMapping("/save")
    // @PreAuthorize("@p.check('base:agreement:save')")
    @Validated
    public void save(@RequestBody AgreementVO agreementVO) throws BusinessException {
        iAgreementService.saveOrUpdate(agreementVO);
    }

    @SysLog("修改协议")
    @ApiOperation("修改协议")
    @PostMapping("/update")
    // @PreAuthorize("@p.check('base:agreement:update')")
    @Validated
    public void update(@RequestBody AgreementVO agreementVO) throws BusinessException {
        iAgreementService.saveOrUpdate(agreementVO);
    }

    @SysLog("删除协议")
    @ApiOperation("删除协议")
    @DeleteMapping("/{agreementId}")
    @ApiImplicitParam(name = "agreementId", value = "协议id", dataType = "long", paramType = "path", required = true)
    // @PreAuthorize("@p.check('base:agreement:delete')")
    public void delete(@PathVariable("agreementId") Long agreementId) throws BusinessException {
        iAgreementService.deleteById(agreementId);
    }
}
